﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.IO;
using System.Web;

namespace LanguageInfo.Common.Utilities
{
	public class PathHelper
	{
		public static string THUMBNAIL_NAIL = "_thumb";

		public static string UrlPrefix
		{
			get
			{
				return string.Format("{0}://{2}{1}", HttpContext.Current.Request.Url.Scheme,
					HttpContext.Current.Request.Url.IsDefaultPort ? string.Empty : ":" + HttpContext.Current.Request.Url.Port.ToString(),
					HttpContext.Current.Request.Url.Host);
			}
		}
		/// <summary>
		/// 获取当前路径，以年月日的结构，如果路径不存在则创建
		/// </summary>
		/// <param name="configName">目录所在位置的配置名</param>
		/// <returns></returns>
		public static string GetPathBaseDate(string configName)
		{
			string path = ConfigurationManager.AppSettings[configName] ?? "~";
			path = string.Format("{0}\\{1}\\{2}\\{3}",
				HttpContext.Current.Server.MapPath(path),
				DateTime.Now.Year,
				DateTime.Now.Month,
				DateTime.Now.Day);
			if (!Directory.Exists(path)) Directory.CreateDirectory(path);
			return path;
		}

		/// <summary>
		/// 获取当前路径，以年月的结构，如果路径不存在则创建
		/// </summary>
		/// <param name="configName">目录所在位置的配置名</param>
		/// <returns></returns>
		public static string GetPathBaseMonth(string configName)
		{
			string path = ConfigurationManager.AppSettings[configName] ?? "~";
			path = string.Format("{0}\\{1}\\{2}",
				HttpContext.Current.Server.MapPath(path),
				DateTime.Now.Year,
				DateTime.Now.Month);
			if (!Directory.Exists(path)) Directory.CreateDirectory(path);
			return path;
		}

		/// <summary>
		/// 获取物理路径相对于根目录的Url
		/// </summary>
		/// <param name="path">绝对路径</param>
		/// <returns></returns>
		public static string GetUrlFromPath(string url)
		{
			if (string.IsNullOrEmpty(url)) return string.Empty;
			string root;
			if (HttpContext.Current != null)
				root = HttpContext.Current.Server.MapPath("~");
			else
				root = AppDomain.CurrentDomain.BaseDirectory;
			return "/" + url.Replace(root, string.Empty).Replace('\\', '/');

		}

		/// <summary>
		/// 获得当前绝对路径
		/// </summary>
		/// <param name="strPath">指定的路径</param>
		/// <returns>绝对路径</returns>
		public static string GetMapPath(string strPath)
		{
			if (HttpContext.Current != null)
			{
				if (strPath.StartsWith(UrlPrefix))
					strPath = strPath.TrimStart(UrlPrefix.ToCharArray());
				if (!(strPath.StartsWith("/") || strPath.StartsWith("~")
					|| strPath.StartsWith("http://", StringComparison.OrdinalIgnoreCase)
					|| strPath.StartsWith("https://", StringComparison.OrdinalIgnoreCase)))
					strPath = string.Concat("~/", strPath);
				return HttpContext.Current.Server.MapPath(strPath);
			}
			else //非web程序引用
			{
				strPath = strPath.Replace("/", "\\");
				if (strPath.StartsWith("\\"))
				{
					strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
				}
				strPath = strPath.TrimStart(new char[] { '~', '\\' });
				return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
			}
		}

		/// <summary>
		/// 得到文件的缩略图文件名(物理路径)
		/// </summary>
		/// <param name="filename">相对路径的图片url</param>
		/// <returns></returns>
		public static string GetUrlThumbPath(string url)
		{
			if (string.IsNullOrEmpty(url)) return string.Empty;
			try
			{
				string fullPath = GetMapPath(url);
				url = "\\" + Path.GetFileNameWithoutExtension(fullPath);
				return fullPath.Replace(url, url + THUMBNAIL_NAIL);
			}
			catch
			{
				return url;
			}
		}

		/// <summary>
		/// 得到文件的缩略图文件的路径(url /物理)
		/// </summary>
		/// <param name="path">略图图路径（url/物理）</param>
		/// <returns></returns>
		public static string GetThumbPath(string path)
		{
			if (string.IsNullOrEmpty(path)) return string.Empty;
			try
			{
				var ps = path.Split();
				string relPath;
				if (ps.Count(s => s.Equals("/")) > ps.Count(s => s.Equals("\\")))
					relPath = string.Concat("/", Path.GetFileNameWithoutExtension(path));
				else
					relPath = string.Concat("\\", Path.GetFileNameWithoutExtension(path));
				return path.Replace(relPath, relPath + THUMBNAIL_NAIL);
			}
			catch
			{
				return path;
			}
		}

		/// <summary>
		/// 判断是否是以文件名是否以_thumb结束
		/// </summary>
		/// <param name="url_path">url或path</param>
		/// <returns></returns>
		public static bool IsThumbnail(string url_path)
		{
			string filename = Path.GetFileNameWithoutExtension(url_path);
			return filename.EndsWith(THUMBNAIL_NAIL);
		}

		/// <summary>
		/// 得到文件的缩略图文件URL
		/// </summary>
		/// <param name="filename">相对路径的图片</param>
		/// <param name="tailString">缩略图的后缀字符</param>
		/// <returns></returns>
		public static string GetThumbUrl(string url, string tailString)
		{
			if (string.IsNullOrEmpty(url)) return string.Empty;
			string thumbFilename = Path.GetFileNameWithoutExtension(url);
			thumbFilename = url.Replace(thumbFilename, thumbFilename + tailString);
			if (File.Exists(GetMapPath(thumbFilename)))
				return thumbFilename;
			else
				return url;
		}

		/// <summary>
		/// 得到文件的缩略图文件URL
		/// </summary>
		/// <param name="filename">相对路径的图片</param>
		/// <returns></returns>
		public static string GetThumbUrl(string url)
		{
			return GetThumbUrl(url, THUMBNAIL_NAIL);
		}

	}
}
